#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define str string
#define all(x) (x).begin(),(x).end()
#define ms(a,t) memset(a,t,sizeof(a))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define db double
const int maxn = 5000+10;
const int mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
const db eps = 1e-7;
int n,m;
int b[maxn],c[maxn][maxn];
int w[maxn];
int st[maxn];
vector<int> f[maxn][maxn][2],h[maxn][maxn];

void inc(int &x,int y) {
	x += y-mod; x += ((x>>31)&mod);
}
int add(int x,int y) {
	x += y-mod;  return x + ((x>>31)&mod); 
}
vector<int> G[maxn];
int g[1050];
ll ans;
int siz[maxn];
void dfs(int x,int fa = 0) {
	siz[x] = 1; 
	for(int i = 1; i <= siz[x]; i++) f[x][i][0].resize((1<<m)),f[x][i][1].resize((1<<m)),f[x][i][2].resize((1<<m));
	for(int p = 0; p < (1<<m); p++) if((p&st[x]) == st[x]) {
		f[x][p][0][1] = 1;
	}
	for(int to : G[x]) if(to != fa) {
		dfs(to,x);
		for(int i = siz[x]+1; i <= siz[x]+siz[to]; i++) f[x][i][0].resize((1<<m)),f[x][i][1].resize((1<<m)),f[x][i][2].resize((1<<m));
		for(int i = siz[x]; i >= 1; i--) {
			for(int j = 1; j <= siz[to]; j++) for(int k = 0; k < 3; k++) {
				for(int p = 0; p < (1<<m); p++) {
					f[x][i+j][k][p] = (f[x][i+j][k][p] + 1ll * f[x][i][k][p] * h[to][j][p]) % mod;
				}
			}
		}
		siz[x] += siz[to];
	}
	for(int v = 1; v <= siz[x]; v++) {
		for(int p = 0; p < (1<<m); p++) g[p] = h[x][v][p];
		for(int i = 0; i < m; i++) for(int p = 0; p < (1<<m); p++) if((p>>i)&1) {
			g[p] = (g[p] - g[(p>>i)&1] + mod) % mod;
		}
		for(int i = 0; i < (1<<m) ;i++) {
			ans += g[i] * w[__builtin_popcount(i)] * b[n-v+1];
		}
	}
}
void Solve() {
	cin >> n >> m; 
	for(int i = 0; i <= n; i++) {
		c[i][0] = 1;
		for(int j = 1; j <= i; j++) {
			c[i][j] = add(c[i-1][j-1],c[i-1][j]);
		}
	}
	b[0] = 1;
	for(int i = 1; i <= n; i++) {
		b[i] = 0;
		for(int j = 1; j <= i; j++) {
			b[i] = (b[i] + 1ll*b[i-j]*c[i-1][j-1])%mod;
		}
//		cerr<<b[i]<<' ';
	}
	for(int i = 1; i <= m; i++) {
		cin >> w[i];
	}
	for(int i = 1,x,y; i <= n; i++) {
		cin >> x;
		for(int j = 1; j <= x; j++) {
			cin >> y; y--;
			st[i] |= (1<<y);
		}
	}
	for(int i = 1,u,v; i < n; i++) {
		cin >> u >> v;
		G[u].pb(v); G[v].pb(u);
	}
	dfs(1);
}
int main() {
//	freopen("water.in","r",stdin);
//	freopen("water.out","w",stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//	int T; cin >> T; while(T--)
	Solve();
	return 0;
}
